package org.edu.modules.research.rproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.utils.ConvertUtils;
import org.edu.modules.research.rbase.entity.VeJkyOrganizeUser;
import org.edu.modules.research.rbase.service.VeJkyFileService;
import org.edu.modules.research.rproject.entity.VeJkyProjectWorkUser;
import org.edu.modules.research.rproject.mapper.VeJkyProjectWorkMapper;
import org.edu.modules.research.rproject.entity.VeJkyProjectWork;
import org.edu.modules.research.rproject.dto.VeJkyProjectWorkDto;
import org.edu.modules.research.rproject.service.VeJkyProjectWorkService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.edu.modules.research.rproject.service.VeJkyProjectWorkUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 实现类
 */
@Service
public class VeJkyProjectWorkServiceImpl extends BaseServiceImpl<VeJkyProjectWork, VeJkyProjectWorkMapper> implements VeJkyProjectWorkService {

    @Autowired
    private VeJkyFileService veJkyFileService;

    @Autowired
    private VeJkyProjectWorkUserService workUserService;

    public IPage<VeJkyProjectWorkDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }
    /**
    * 查询实体类
    */
    public VeJkyProjectWorkDto findDtoById(String id) {
        VeJkyProjectWork entity = super.findById(id);
        VeJkyProjectWorkDto dto = ConvertUtils.convertToDto(entity, VeJkyProjectWorkDto.class);

        LambdaQueryWrapper<VeJkyProjectWorkUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(VeJkyProjectWorkUser::getJkyUserId);
        lambdaQueryWrapper.eq(VeJkyProjectWorkUser::getWorkId,dto.getId());
        List<VeJkyProjectWorkUser> veJkyOrganizeUsers = workUserService.listByWrapper(lambdaQueryWrapper);
        List<Integer> userIds = new ArrayList<>();
        for (VeJkyProjectWorkUser user: veJkyOrganizeUsers) {
            userIds.add(user.getJkyUserId());
        }
        dto.setUserIds(userIds);
        
        return dto;
    }

    /**
    * 新增或者修改
    */
    public void insertOrUpdate(VeJkyProjectWorkDto dto,Boolean isAdd){
        Date now = new Date();
        if(isAdd){
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            dto.setCreateUid(sysUser.getId());
            dto.setCreateName(sysUser.getRealname());
            dto.setCreateTime(now);
            dto.setProgress(0);
            super.insert(dto);
        }else{
            workUserService.deleteByPId(dto.getId());
            super.update(dto);
        }

        List<VeJkyProjectWorkUser> users = new ArrayList<>();
        for (Integer userId:dto.getUserIds()) {
            VeJkyProjectWorkUser user = new VeJkyProjectWorkUser();
            user.setJkyUserId(userId);
            user.setWorkId(dto.getId());
            user.setProjectId(dto.getProjectId());
            user.setCreateTime(now);
            users.add(user);
        }
        workUserService.insert(users);

        veJkyFileService.updateFiles(dto.getFileIds(),dto.getId()+"","projectWork");
    }

}
